<?php
class Index extends Action {
	private $limit = 3;
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		
		$where = Model::where_build ( array ('name like' ), $data, array ('name' => 'like' ) );
		
		$themeModel = new WebThemeModel ();
		
		$themeModel->sort ( array ('name', 'd' ) );
		
		$themes = $themeModel->where ( $where )->count ( true )->retrieve ();
		
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		
		if ($themes) {
			$data ['items'] = $themes;
			$data ['countTotal'] = $themes->countTotal;
		}
		$data ['selected'] = array (0 => '', '1' => '<span class="label">默认</span>' );
		$data ['nodes'] = json_encode ( WebTemplateModel::getTemplateTreeData () );
		bind ( 'theme_options', array ($this, 'theme_options' ), 10, 2 );
		bind ( 'theme_templates', array ($this, 'theme_templates' ), 10, 2 );
		return new SmartyView ( $data, 'theme/Index' );
	}
	public function theme_options($ops, $item) {
		if (! $item ['selected']) {
			$ops .= '<a title="设为默认" class="theme-default" href="./?Ctlr=SetDefault&type=theme&tid=' . $item ['theme_id'] . '"><i class="icon-star"></i></a>';
		}
		$ops .= '<a title="编辑" href="./?Ctlr=EditTheme&tid=' . $item ['theme_id'] . '"><i class="icon-edit"></i></a>';
		$ops .= '<a title="删除" onclick="return confirm(\'确定要删除该主题?\');" href="./?Ctlr=DelTheme&tid=' . $item ['theme_id'] . '"><i class="icon-trash"></i></a>';
		return $ops;
	}
	public function theme_templates($tr, $item) {
		static $tplM = null, $types = null;
		if ($tplM == null) {
			$tplM = new WebTemplateModel ();
			$types = apply_filter ( 'get_page_types', array () );
		}
		$tpls = $tplM->where ( array ('theme_id' => $item ['theme_id'] ) )->retrieve ();
		$tpl_types = $tpls->toArray('page_type','template_file');
		$tpl_ids = $tpls->toArray('page_type','template_id');
		foreach ($types as $type => $name){//所有页面类型
			if(isset($tpl_types[$type])){
				$tr .= "<tr class=\"tpls tpl_{$item ['theme_id']} hide\">
							<td></td><td></td>
							<td>{$name}</td>
							<td class=\"tpl_f\">{$tpl_types[$type]}</td>
							<td class=\"tools\">
								<a class=\"set_tpl\" title=\"设置模板\" href=\"./?Ctlr=SetTpl&type={$type}&tpid={$tpl_ids[$type]}\">
									<i class=\"icon-file\"></i>
								</a>
							</td>
						</tr>";
			}else{
				$tr .= "<tr class=\"tpls tpl_{$item ['theme_id']} hide\">
							<td></td>
							<td></td>
							<td>{$name}</td>
							<td class=\"tpl_f\"></td>
							<td class=\"tools\">
								<a class=\"set_tpl\" title=\"设置模板\" href=\"./?Ctlr=SetTpl&type={$type}&thid={$item['theme_id']}\">
									<i class=\"icon-file\"></i>
								</a>
							</td>
						</tr>";
			}			
		}		
		return $tr;
	}
}